import { t } from 'i18next'
import { MtEntity } from '../mt'
import { Poll, PollEntity, revert_poll } from '../poll/interface'
export interface VmpEntity {
	/** 画面合成模式
	 * 1-定制画面合成;
	 * 2-自动画面合成;
	 * 3-自动画面合成批量轮询(预留);
	 * 4-定制画面合成批量轮询; */
	mode: number
	/** 预案名称 */
	name: string
	/** 预案id */
	id: string
	/** 画面合成风格 0-自动画面合成 其他 */
	layout: number
	/** 显示别名 */
	show_mt_name: 0 | 1
	/** 识别声音来源 */
	voice_hint: 0 | 1
	/** 广播 */
	broadcast: 0 | 1
	/** 自动画面合成n-1模式 */
	except_self: 0 | 1
	/** 字体信息 */
	mt_name_style?: FontEntity
	/** 画面合成成员信息 */
	members?: VmpMemberEntity[]
	/** 轮询信息 */
	poll?: PollEntity
	/** 状态(cometd推送中包含) 0-未开启; 1-正在进行 */
	state?: 0 | 1
}
export interface FontEntity {
	/** 字体颜色 （0-小 1-中 2-大） */
	font_color?: string
	/** 字体大小  */
	font_size?: number
	/** 字体位置  （0-左上角 1-左下角 2-右上角 3-右下角 4-底部中间）*/
	position?: number
}
export type VmpMemberEntity = {
	/** 跟随类型 1-会控指定；2-发言人跟随；3-管理方跟随；4-会议轮询跟随；6-单通道轮询；7-内容共享跟随 */
	member_type: number
	chn_idx: number
	/** 单通道轮询成员独有 */
	poll?: PollEntity
	mt_id?: string
	account?: string
	account_type?: 2 | 1 | 6 | 4 | 5 | 7 | 8
	name?: string
	portrait?: string
	conf_e164?: string
	bitrate?: number
	mt_key?: string
	/** 终端类型 1-普通终端; 3-电话终端; */
	mt_type?: number
	online?: 0 | 1
	e164?: string
	mt_ip?: string
	stream?: 0 | 1
	mute?: 0 | 1
	video_src?: 0 | 1
}
export class Vmp {
	id: string
	name: string
	mode: number
	layout: number
	show_mt_name: 0 | 1
	voice_hint: 0 | 1
	broadcast: 0 | 1
	except_self: 0 | 1
	mt_name_style?: FontEntity
	members: VmpMember[]
	poll?: Poll
	state?: 0 | 1
	constructor(vmp: VmpEntity) {
		this.id = vmp.id
		this.name = vmp.name
		this.mode = vmp.mode
		this.layout = vmp.layout
		this.show_mt_name = vmp.show_mt_name
		this.voice_hint = vmp.voice_hint
		this.broadcast = vmp.broadcast
		this.except_self = vmp.except_self
		this.state = vmp.state
		this.mt_name_style = vmp.mt_name_style
		this.members = vmp.members ? vmp.members.map((item) => new VmpMember(item)) : []
		this.poll = vmp.poll ? new Poll(vmp.poll) : undefined
	}
}
export function revert_vmp(vmp: Vmp): VmpEntity {
	return {
		id: vmp.id,
		name: vmp.name,
		mode: vmp.mode,
		layout: vmp.layout,
		show_mt_name: vmp.show_mt_name,
		voice_hint: vmp.voice_hint,
		broadcast: vmp.broadcast,
		except_self: vmp.except_self,
		mt_name_style: vmp.mt_name_style,
		members: vmp.members.map((item) => revert_vmp_member(item)),
		poll: vmp.poll ? revert_poll(vmp.poll) : undefined,
		state: vmp.state,
	}
}
export class VmpMember {
	member_type: number
	chn_idx: number
	mt_id?: string
	account?: string
	account_type?: 2 | 1 | 6 | 4 | 5 | 7 | 8
	INITIAL_NAME?: string
	portrait?: string
	conf_e164?: string
	bitrate?: number
	mt_key?: string
	mt_type?: number
	online?: 0 | 1
	e164?: string
	mt_ip?: string
	stream?: 0 | 1
	mute?: 0 | 1
	poll?: Poll
	video_src?: 0 | 1
	constructor(member: VmpMemberEntity) {
		this.member_type = member.member_type ?? 1
		this.chn_idx = member.chn_idx ?? 0
		this.mt_id = member.mt_id
		this.account = member.account
		this.account_type = member.account_type
		this.INITIAL_NAME = member.name?.replace(/&nbsp;/g, ' ') ?? ''
		this.portrait = member.portrait
		this.conf_e164 = member.conf_e164
		this.bitrate = member.bitrate
		this.mt_key = member.mt_key
		this.mt_type = member.mt_type
		this.online = member.online
		this.e164 = member.e164
		this.mt_ip = member.mt_ip
		this.stream = member.stream
		this.mute = member.mute || 0
		this.poll = member.poll ? new Poll(member.poll) : undefined
		this.video_src = member.video_src
		//当是单通道轮询时，当前轮询终端数据信息放在 poll 里的 cur_poll_mt， 将它放出来
		if (member.member_type === 6 && member.poll && member.poll.cur_poll_mt) {
			let _tmp = member.poll.cur_poll_mt
			this.mt_id = _tmp.mt_id
			this.account = _tmp.account
			this.account_type = _tmp.account_type
			this.INITIAL_NAME = _tmp.name?.replace(/&nbsp;/g, ' ') ?? ''
			this.portrait = _tmp.portrait
			this.conf_e164 = _tmp.conf_e164
			this.bitrate = _tmp.bitrate
			this.mt_key = _tmp.mt_key
			this.mt_type = _tmp.mt_type
			this.online = _tmp.online
			this.e164 = _tmp.e164
			this.mt_ip = _tmp.mt_ip
			this.stream = _tmp.stream
			this.mute = _tmp.mute || 0
			this.video_src = _tmp.video_src
		}
	}
	get name(): string {
		if (this.INITIAL_NAME && this.INITIAL_NAME.indexOf(t('[多流]')) === -1 && this.stream && this.online) {
			return t('[多流]') + this.INITIAL_NAME
		}
		return this.INITIAL_NAME ?? ''
	}
}
export function revert_vmp_member(member: VmpMember): VmpMemberEntity {
	return {
		member_type: member.member_type,
		chn_idx: member.chn_idx,
		mt_id: member.mt_id,
		account: member.account,
		account_type: member.account_type,
		name: member.INITIAL_NAME,
		portrait: member.portrait,
		conf_e164: member.conf_e164,
		bitrate: member.bitrate,
		mt_key: member.mt_key,
		mt_type: member.mt_type,
		online: member.online,
		e164: member.e164,
		mt_ip: member.mt_ip,
		stream: member.stream,
		mute: member.mute,
		poll: member.poll ? revert_poll(member.poll) : undefined,
		video_src: member.video_src,
	}
}
export enum VmpTypeEnum {
	'发言方跟随' = 2,
	'管理方跟随' = 3,
	'会议轮询跟随' = 4,
	'单通道轮询' = 6,
	'内容共享跟随' = 7,
}
export interface SearchListItem {
	id: string
	mt_key: string
	parent_id: string
	conf_id: string
	cascade_id: string
	name: string
	conf_level: 1 | 2 | 3
	online?: 0 | 1
	speaker?: 0 | 1
	mts: MtEntity[]
}
